package com.wenbiao.springcloud.contrller;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wenbiao.springcloud.hystrix.MyHystrixCommand;
import com.wenbiao.springcloud.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class WebController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/web/hello")
    public String hello(){
        //逻辑判断省略

        //调用springcloud服务提供的服务
//        return restTemplate.getForEntity("http://localhost/service/hello",String.class).getBody();
        return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/hello",String.class).getBody();
    }


    @RequestMapping(value = "/web/user")
    public User user(){
        //逻辑判断省略

        //调用springcloud服务提供的服务
        ResponseEntity<User>  responseEntity = restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/user",User.class);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getBody());
        System.out.println(responseEntity.getHeaders());
        return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/user",User.class).getBody();
    }

    @RequestMapping(value = "/web/getUser")
    public User getUser(){
        String [] strArrays={"1008","二胖","183719371","18"};
        //调用springcloud服务提供的服务
        ResponseEntity<User>  responseEntity = restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/getUser?id={0}&name={1}&phone={2}&age={3}",User.class,strArrays);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getBody());
        System.out.println(responseEntity.getHeaders());
        return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/getUser?id={0}&name={1}&phone={2}&age={3}",User.class,strArrays).getBody();
    }

    @RequestMapping(value = "/web/getUser1")
    public User getUser1(){
        Map<String,Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("id",1);
        paramMap.put( "name","张三");
        paramMap.put( "phone","1781738613");
        paramMap.put( "age","23");
        ResponseEntity<User>  responseEntity = restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/getUser?id={id}&name={name}&phone={phone}&age={age}", User.class, paramMap);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getBody());
        System.out.println(responseEntity.getHeaders());
        return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/getUser?id={id}&name={name}&phone={phone}&age={age}", User.class, paramMap).getBody();
    }


    @RequestMapping(value = "/web/addUser")
    public User addUser(){
        //要传的表单信息，参数数据
        MultiValueMap<String,Object> dataMap = new LinkedMultiValueMap<>();
        dataMap.add("id",1);
        dataMap.add("name","张三");
        dataMap.add("phone","1781738613");
        dataMap.add("age","23");

        ResponseEntity<User>  responseEntity = restTemplate.postForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/addUser",dataMap, User.class);
        User  user = restTemplate.postForObject("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/addUser",dataMap, User.class);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getBody());
        System.out.println(responseEntity.getHeaders());
        System.out.println("=================");
        System.out.println(user.toString());
        return restTemplate.postForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/addUser",dataMap, User.class).getBody();
    }


    @RequestMapping(value = "/web/updateUser")
    public String updateUser(){
        //要传的表单信息，参数数据
        MultiValueMap<String,Object> dataMap = new LinkedMultiValueMap<>();
        dataMap.add("id",1);
        dataMap.add("name","张三");
        dataMap.add("phone","1781738613");
        dataMap.add("age","23");
        restTemplate.put("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/updateUser",dataMap);
        return "success";
    }

    @RequestMapping(value = "/web/deleteUser")
    public String deleteUser(){
        //要传的表单信息，参数数据
        Map<String,Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("id",1);
        paramMap.put( "name","张三");
        paramMap.put( "phone","1781738613");
        paramMap.put( "age","23");

        String [] strArrays={"1008","二胖","183719371","18"};

        restTemplate.delete("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/deleteUser?id={0}&name={1}&phone={2}&age={3}",strArrays);
        restTemplate.delete("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/deleteUser?id={id}&name={name}&phone={phone}&age={age}",paramMap);
        return "success";
    }


    @RequestMapping(value = "/web/hystrix")
    @HystrixCommand(fallbackMethod = "error" ,commandProperties = {
    @HystrixProperty(name= "execution.isolation.thread.timeoutInMilliseconds" , value = "3500")
    },ignoreExceptions = RuntimeException.class)
    public String hystrix(){
        //模拟运行时异常
//        int a= 10 / 0;
        return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/hello",String.class).getBody();
    }

    public String error(Throwable throwable){
        System.out.println("异常"+throwable.getMessage());
        return "error";
    }

    @RequestMapping(value = "/web/hystrix2")
    public String hystrix2() throws ExecutionException, InterruptedException {
        MyHystrixCommand myHystrixCommand=new MyHystrixCommand(
                com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(
                        HystrixCommandGroupKey.Factory .asKey(""))
                ,restTemplate);
        //同步调用（该方法执行后，会等待远程的返回结果，拿到了远程的返回结果该方法才会返回，然后代码继续往下执行）
//        String str = myHystrixCommand.execute();
        //异步调用(该方法执行后不会马上有远程的返回结果，以后才会有结果)
        Future<String> future =myHystrixCommand.queue();
        //写一些业务逻辑

        //阻塞的方法，直到拿到结果
        String str1=future.get();
        //写一些业务逻辑

        return str1;
    }
}
